package com.example.smartcity_poverty.ui.AllService.SmartBus.adapter

import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.context
import com.example.smartcity_poverty.Base.BaseRecyclerAdapter
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.ServiceBean.SmartBus.BusPathAllModel
import com.example.smartcity_poverty.logic.ServiceBean.SmartBus.BusPathModel
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.SmartBus.activity.SmartBusOneActivity
import kotlinx.android.synthetic.main.re_bus_home_all.*
import kotlinx.android.synthetic.main.re_bus_home_recycler.*
import kotlinx.coroutines.Dispatchers

/**
 *  created by ikun
 *  ANDROID STUDIO - version 4.0
 *  email: jinkunwei41@gmail.com
 * --------------------------------
 *  智慧巴士主页列表适配器
 **/
class BusHomeRecyclerAdapter (
    val list: MutableList<BusPathModel.RowsBean>,
    layoutId: Int
) : BaseRecyclerAdapter<BusPathModel.RowsBean>(list, layoutId){
    override fun setData(
        view: BaseViewHolder.ViewFind,
        data: BusPathModel.RowsBean,
        position: Int,
        holder: BaseViewHolder
    ) {
        val d = list[position]
        val fv = holder.viewFind
        val con = holder.viewFind.view?.context

        // 填充数据
        fv.busHomeRvName.text = d.name
        fv.busHomeRvStartName.text = d.first
        fv.busHomeRvEndName.text = d.end
        fv.busHomeRvStartTime.text = "首发时间: ${d.startTime}"
        fv.busHomeRvEndTime.text = "末班时间: ${d.endTime}"
        fv.busHomeRvSize.text = "${d.mileage}里程"

        // 点击事件
        holder.itemView.setOnClickListener {
            con?.startActivity(Intent(con, SmartBusOneActivity::class.java).putExtra("id", d.id.toString()))
        }

        var ff = false
        fv.busHomeIsOKAll.setOnClickListener {
            // 发起网络请求获取更多路线
            // 存储路线数据
            val array = ArrayList<BusPathAllModel.RowsBean>()
            coroutine(Dispatchers.Main){
                val result = api.getBusPathAll(d.id)
                if (result.code == 200){
                    result.rows?.let {
                        array.addAll(it)
                    }
                    // 配置适配器
                    fv.busHomeRvAll.apply {
                        this.layoutManager = LinearLayoutManager(context).apply {
                            this.orientation = LinearLayoutManager.HORIZONTAL
                        }
                        this.adapter = BusAll(array, R.layout.re_bus_home_all)
                    }
                }
            }
            if (!ff){
                fv.busHomeRvAllLayout.visibility = View.VISIBLE
                ff = true
            }else {
                fv.busHomeRvAllLayout.visibility = View.GONE
                ff = false
            }
        }
    }

    /**
     * 更多路线适配器
     */
    class BusAll(
        val list: MutableList<BusPathAllModel.RowsBean>,
        layoutId: Int
    ): BaseRecyclerAdapter<BusPathAllModel.RowsBean>(list, layoutId){
        override fun setData(
            view: BaseViewHolder.ViewFind,
            data: BusPathAllModel.RowsBean,
            position: Int,
            holder: BaseViewHolder
        ) {
            // 填充数据
            val d = list[position]
            val fv = holder.viewFind

            // 填充数据
            fv.busHomeAllTitle.text = d.name
        }

    }
}